<template>
  <div>
    <el-form ref="form" :model="user" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="user.loginName" readonly></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="user.userName"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio-group v-model="user.sex">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="user.email" readonly></el-input>
      </el-form-item>
      <el-form-item label="身份证号">
        <el-input v-model="user.identityCode"></el-input>
      </el-form-item>
      <el-form-item label="手机">
        <el-input v-model="user.mobile"></el-input>
      </el-form-item>
      <el-form-item label="用户类型">
        <el-select v-model="user.type">
          <el-option label="普通用户" :value="0"></el-option>
          <el-option label="管理员" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save()">保存</el-button>
        <el-button @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "userEdit",
  data() {
    return {
      user: {
        id: "",
        loginName: "",
        userName: "",
        sex: "",
        email: "",
        identityCode: "",
        mobile: "",
        type: "",
      },
    };
  },
  methods: {
    back() {
      this.$router.push("/managerIndex/manageUser");
    },
    showInfo() {
      this.$axios
        .get(`api/user/find/${this.user.id}`)
        .then((response) => {
          this.user = response.data.User;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    save() {
      this.$axios
        .post("/api/user/edit", {
          id: this.user.id,
          loginName: this.user.loginName,
          userName: this.user.userName,
          sex: this.user.sex,
          identityCode: this.user.identityCode,
          email: this.user.email,
          mobile: this.user.mobile,
          type: this.user.type,
        })
        .then((response) => {
          if (response.data.editStatus) {
            alert("修改成功");
            this.$router.push("/managerIndex/manageUser");
          } else {
            alert("修改失败");
          }
        });
    },
  },
  mounted() {
    this.user.id = this.$route.query.id;
    this.showInfo();
  },
};
</script>

<style></style>
